package br.ufal.tci.nexos.arcolive.beans;

import java.awt.Component;
import java.io.IOException;
import java.io.Serializable;

import javax.media.ControllerListener;
import javax.media.Manager;
import javax.media.MediaLocator;
import javax.media.NoDataSourceException;
import javax.media.NoProcessorException;
import javax.media.Processor;
import javax.media.control.TrackControl;
import javax.media.protocol.DataSource;

/**
 * @author <a href="mailto:felipe@labpesquisas.tci.ufal.br">Felipe Barros Pontes</a>.
 * @author <a href="mailto:leandro@labpesquisas.tci.ufal.br">Leandro Melo de
 *         Sales</a>.
 */
public class ArCoLIVEProcessor implements Serializable {

	private Processor processor;

	private DataSource dataSource;

	private MediaLocator mediaLocator;

	/**
	 * 
	 */
	public ArCoLIVEProcessor() {
	}

	/**
	 * @throws CannotCreateProcessorException
	 */
	public void createProcessor() throws CannotCreateProcessorException {
		String message = "Cannot create processor: ";
		try {
			this.dataSource = Manager.createDataSource(this.mediaLocator);
			this.processor = Manager.createProcessor(this.dataSource);
		} catch (NoProcessorException e) {
			throw new CannotCreateProcessorException(message + e.getMessage());
		} catch (IOException e) {
			throw new CannotCreateProcessorException(message + e.getMessage());
		} catch (NoDataSourceException e) {
			throw new CannotCreateProcessorException(message + e.getMessage());
		}
	}

	/**
	 * @param dataSource
	 * @throws CannotCreateProcessorException
	 */
	public void createProcessor(DataSource dataSource)
			throws CannotCreateProcessorException {
		String message = "Cannot create processor: ";
		this.dataSource = dataSource;
		try {
			this.processor = Manager.createProcessor(this.dataSource);
		} catch (NoProcessorException e) {
			throw new CannotCreateProcessorException(message + e.getMessage());
		} catch (IOException e) {
			throw new CannotCreateProcessorException(message + e.getMessage());
		}
	}

	/**
	 * @return
	 */
	public TrackControl[] getTrackControls() {
		return this.processor.getTrackControls();
	}

	/**
	 * @return
	 */
	public DataSource getDataSource() {
		return this.dataSource;
	}

	/**
	 * @return
	 */
	public Component getVisualComponent() {
		return this.processor.getVisualComponent();
	}

	/**
	 * @return
	 */
	public Component getControlComponent() {
		return this.processor.getControlPanelComponent();
	}

	/**
	 * 
	 */
	public void start() {
		this.processor.start();
	}

	/**
	 * @param mediaLocator
	 */
	public void setMediaLocator(MediaLocator mediaLocator) {
		this.mediaLocator = mediaLocator;
	}

	/**
	 * 
	 */
	public void eject() {
		this.processor.close();
		this.processor.deallocate();
	}

	/**
	 * 
	 */
	public void stop() {
		this.processor.stop();
	}

	/**
	 * @param listener
	 */
	public void addControllerListener(ControllerListener listener) {
		this.processor.addControllerListener(listener);
	}

	/**
	 * @param rate
	 */
	public void setRate(float rate) {
		this.processor.setRate(rate);
	}

}